//
//  DatePickerView.m
//  Hai
//
//  Created by admin on 15/11/17.
//  Copyright (c) 2015年 wangzijian. All rights reserved.
//

#import "CalendarView.h"
@implementation CalendarView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
}
- (instancetype)initWithCurrentDate:(NSDate *)date{
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 1;
        self.date = date;
        [self setFrame:CGRectMake(0,0, kScreenWidth,0)];
      
    }
    return self;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame: self.bounds];
        _backgroundView.backgroundColor = [UIColor whiteColor];
        _backgroundView.alpha = 0;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDatePickerView)];
        [_backgroundView addGestureRecognizer:tapGesture];
    }
    
    [self addSubview:_backgroundView];
    
    return _backgroundView;
}

//时间选择view
- (UIView *)datePickerView {
    if (!_datePickerView) {
        _datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.frame.size.width, 0)];
        _datePickerView.backgroundColor = [UIColor whiteColor];
        _datePickerView.clipsToBounds = YES;
        
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 245, self.frame.size.width/2, 30)];
        cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
     //   cancelButton.backgroundColor = LightRed;
    //    cancelButton.layer.cornerRadius = 5.0f;
        [cancelButton setTitle:@"确定" forState:UIControlStateNormal];
        [cancelButton setTitleColor:BlackColor forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(selectCurrentDate) forControlEvents:UIControlEventTouchUpInside];
        [_datePickerView addSubview:cancelButton];
        
        UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2, 245, self.frame.size.width/2, 30)];
        okButton.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
      // okButton.backgroundColor = LightRed;
       okButton.layer.cornerRadius = 5.0f;
        [okButton setTitle:@"取消" forState:UIControlStateNormal];
        [okButton setTitleColor:BlackColor forState:UIControlStateNormal];
        [okButton addTarget:self action:@selector(cancelSelectCurrentDate) forControlEvents:UIControlEventTouchUpInside];
        [_datePickerView addSubview:okButton];
        
        [_datePickerView addSubview:self.datePicker];
    }
    
    [self addSubview:_datePickerView];
    
    return _datePickerView;
}
//时间选择控件
- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        CGRect frame = _datePicker.frame;
        frame.origin = CGPointMake(0,25);
        _datePicker.frame = CGRectMake(0, 25, kScreenWidth, 220);
        [self.datePicker setTimeZone:[NSTimeZone localTimeZone]];
        NSLocale*locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        self.datePicker.locale=locale;
        NSDate *currentTime  = [NSDate date];
        [self.datePicker setDate:currentTime animated:YES];
        [self.datePicker setMaximumDate:currentTime];
 //       NSDate*datebefore = [Expand StringtoNSDate:[NSString stringWithFormat:@"2015-05-01"]];
  //      [self.datePicker setMinimumDate:datebefore];
        self.datePicker.backgroundColor=[UIColor whiteColor];
        
    }
    
    return _datePicker;
}
- (void)showDatePickerView {
    [UIView animateWithDuration:0.25 animations:^{
        [self setFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight-64)];
        self.backgroundView.alpha = 0.2;
        self.backgroundView.frame = CGRectMake(0,0, kScreenWidth, kScreenHeight-64);
        self.datePickerView.frame = CGRectMake(0,kScreenHeight - 344, kScreenWidth,344);

    }];
}
- (void)hideDatePickerView {
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundView.alpha = 0;
        self.frame = CGRectMake(0,324,kScreenWidth,0);
        self.backgroundView.frame = CGRectMake(0,0, kScreenWidth, 0);
        self.datePickerView.frame = CGRectMake(0,0, kScreenWidth, 0);
        
    } completion:^(BOOL finished) {
        [self.backgroundView removeFromSuperview];
        [self.datePickerView removeFromSuperview];
        [self removeFromSuperview];
    }];
}
- (void)setCurrentDate:(NSDate *)dade{
    self.date =dade;
    
}
//选择当前日期
- (void)selectCurrentDate {
    [self setCurrentDate:self.datePicker.date];
    
    if ([self.dateDelegate respondsToSelector:@selector(sendDate:)]) {
        [self.dateDelegate sendDate:self.date ];
    }
    [self hideDatePickerView];
}

//取消选择当前日期
- (void)cancelSelectCurrentDate {
    [self hideDatePickerView];
}

@end
